﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;

namespace MSBTC.WinForms
{
    public partial class CategoryForm : Form
    {
        MDIParent mdipar = new MDIParent();
        private string Datamanip;
        PMSContext dbcontext = new PMSContext();
        Category dbcat = new Category();
        FormHelpers help = new FormHelpers();
        private int rowselect;

        public CategoryForm()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CategoryForm_Load(object sender, EventArgs e)
        {
            RefreshFields();
            RefreshDB();
        }
        
        #region functions
        private void RefreshDB()
        {
            var cat = (from c in dbcontext.Categories
                       select c).ToList();

            LVCatInfo.Items.Clear();
            if (cat != null)
            {
                
                foreach (var sqlCat in cat)
                {
                    ListViewItem lvitems = LVCatInfo.Items.Add(sqlCat.Categoryid_PK.ToString());
                    lvitems.SubItems.Add(sqlCat.Category_Name.ToString());
                    lvitems.SubItems.Add(sqlCat.Description.ToString());
                    
                }
            }
            this.Refresh();
        }

        private void ClearFields()
        {
            txtID.Clear();
            txtCatName.Clear();
            txtCatDesc.Clear();
            btnCatDel.Enabled = false;
            btnCatEdit.Enabled = false;
            btnCatSave.Enabled = false;
            btnCatNew.Enabled = true;
        }

        private void DisableFields()
        {
            txtCatName.Enabled = false;
            txtCatDesc.Enabled = false;

        }

        private void EnableFields()
        {
            txtCatName.Enabled = true;
            txtCatDesc.Enabled = true;

        }

        private void RefreshFields()
        {
            DisableFields();
            ClearFields();
            RefreshDB();

            btnCatNew.Enabled = true;
            btnCatDel.Enabled = false;
            btnCatEdit.Enabled = false;
            btnCatSave.Enabled = false;
        }
        #endregion

        private void LVCatInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            rowselect = LVCatInfo.FocusedItem.Index;


            txtID.Text = LVCatInfo.Items[rowselect].SubItems[0].Text;
            txtCatName.Text = LVCatInfo.Items[rowselect].SubItems[1].Text;
            txtCatDesc.Text = LVCatInfo.Items[rowselect].SubItems[2].Text;
            

            btnCatDel.Enabled = true;
            btnCatEdit.Enabled = true;
            btnCatSave.Enabled = false;
            btnCatNew.Enabled = true;
        }

        private void btnCatNew_Click(object sender, EventArgs e)
        {
            Datamanip = "Add";
            EnableFields();
            ClearFields();

            btnCatSave.Enabled = true;
            btnCatNew.Enabled = false;
            LVCatInfo.Enabled = false;

        }

        private void btnCatSave_Click(object sender, EventArgs e)
        {
            if (txtCatName.Text == "" || txtCatDesc.Text == string.Empty)
            {
                MessageBox.Show("Please fill up the empty fields", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //validation for special character inputs
            else if (help.SpecChar(txtCatName.Text, txtCatName) == false)
            {

                MessageBox.Show("Special Characters are not allowed", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {

                if (Datamanip == "Add")
                {
                    var add = (from c in dbcontext.Categories
                               where c.Category_Name == txtCatName.Text
                               select c).ToList();

                    if (add.Count == 0)
                    {
                        dbcat.Category_Name = txtCatName.Text;
                        dbcat.Description = txtCatDesc.Text;

                        dbcontext.Entry(dbcat).State = EntityState.Added;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();

                    }
                    else
                    {
                        foreach (var c in add)
                        {
                            if (c.Category_Name == txtCatName.Text && c.Description == txtCatDesc.Text)
                            {
                                DialogResult YN = MessageBox.Show("You've entered an existing client, do you want to replace it?", "Data Exist", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                                if (YN == DialogResult.Yes)
                                {
                                    var replace = dbcontext.Categories.Find(c.Categoryid_PK);
                                    replace.Category_Name = txtCatName.Text;
                                    replace.Description = txtCatDesc.Text;


                                    dbcontext.Entry(replace).State = EntityState.Modified;
                                    dbcontext.SaveChanges();

                                    MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    RefreshFields();
                                }
                            }
                            else
                            {
                                dbcat.Category_Name = txtCatName.Text;
                                dbcat.Description = txtCatDesc.Text;


                                dbcontext.Entry(dbcat).State = EntityState.Added;
                                dbcontext.SaveChanges();

                                MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                RefreshFields();
                                RefreshDB();
                            }
                        }
                    }
                    LVCatInfo.Enabled = true;
                }

                else if (Datamanip == "Edit")
                {
                    DialogResult YN = MessageBox.Show("Do you want to replace it?", "Modify", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (YN == DialogResult.Yes)
                    {
                        var replace = dbcontext.Categories.Find(Convert.ToInt32(txtID.Text));
                        replace.Category_Name = txtCatName.Text;
                        replace.Description = txtCatDesc.Text;


                        dbcontext.Entry(replace).State = EntityState.Modified;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();
                    }
                }
            }
        }

        private void btnCatDel_Click(object sender, EventArgs e)
        {
            DialogResult YN = MessageBox.Show("Do you want to Delete it?", "Modify", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (YN == DialogResult.Yes)
            {
                var del = dbcontext.Categories.Find(Convert.ToInt32(txtID.Text));
                if (del != null)
                {
                    dbcontext.Entry(del).State = EntityState.Deleted;
                    dbcontext.SaveChanges();
                }

            }
            RefreshFields();
            RefreshDB();
        }

        private void btnCatEdit_Click(object sender, EventArgs e)
        {
            Datamanip = "Edit";

            EnableFields();

            btnCatSave.Enabled = true;
            btnCatNew.Enabled = false;
            btnCatDel.Enabled = false;
            btnCatEdit.Enabled = false;
        }

        private void txtCatSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtCatSearch.Text == "")
            {
                RefreshDB();
            }
            else
            {
                //retrieve the same item name search by user
                var purch = (from p in dbcontext.Categories
                             where p.Category_Name.Contains(txtCatSearch.Text)
                             select p).ToList();

                LVCatInfo.Items.Clear();
                if (purch.Count != 0)
                {

                    {

                        foreach (var sqlCat in purch)
                        {
                            ListViewItem lvitems = LVCatInfo.Items.Add(sqlCat.Categoryid_PK.ToString());
                            lvitems.SubItems.Add(sqlCat.Category_Name.ToString());
                            lvitems.SubItems.Add(sqlCat.Description.ToString());
                          
                        }
                    }

                }

            }
        }

        private void btnCatRefresh_Click(object sender, EventArgs e)
        {
            RefreshFields();
            LVCatInfo.Enabled = true;
        }

        private void btnCatClear_Click(object sender, EventArgs e)
        {
            ClearFields();
        }
    }
}
